//
//  TicketModel.swift
//  ANOA
//
//  Created by WB Zhou on 2017/4/7
//  Copyright (c) aonong. All rights reserved.
//

import Foundation

public struct TicketModel: SwiftyJSONAble {
    
    // MARK: Declaration for string constants to be used to decode and also serialize.

    
    // MARK: Properties
    public var fBuyeraddress: String?
    public var fBuyeracount: String?
    public var sCheckInfo: String?
    
    
    public var fUseState: String?
    public var fInvoicetypeNm: String?
    public var fSalername: String?
    public var fInvoicecode: String?
    public var fInvoicedate:String?
    public var fOwnerId: String?
    public var fBuyertaxno: String?
    public var idKey: String?
    public var fTaxamount: String?
    public var fSaleraddress: String?
    public var fSaleracount: String?
    public var fInvoiceamount: String?
    public var fOwnerNm: String?
    public var fLegalizestate: String?
    public var fPassenger: String?
    public var fCreatetime: String?
    public var fSalertaxno: String?
    public var fInvoiceno: String?
    public var fTrainnumber: String?
    public var fBuyername: String?
    public var fArrivecity: String?
    public var fInvoicestatus: String?
    public var fDepartcity: String?
    public var fInvoicetypeId: String?
    public var fLegalizedate: String?
    public var fIseinvoice: String?
    public var fTotalamount: String?
    public var fCheckstate: String?
    public var fVerifycode: String?
    public var resultTip: String?
    public var resultCode: String?
    public var fInvoiceId: String?
    public var fRecordnum: String?
    public var fRemark: String?
    public var fCompany: Int?
    public var fBxtypeId = 6
    public var fBxtypeNm = "其他"
    public var vouType:String?
    public var inputNum:String?
    public var fOrgId:String?
    public var fOrgNm:String?
    public var fLabel:String?
    public var withSource = false
    public var fpNumber:String?
    public var errorMsg:String?
    public var sNo:String?
    public var fTaxRate: String?
    public var fCheckFailMessage: String?
    
    public var details: [TicketDetailModel]?
    /// 允许有选择的功能
    public var withSelect = false
    /// 开始在选择了
    public var startSelect = false
    /// 合法的可以选择的发票
    public var selectable = true
    /// 不能选择的原因
    public var selectDisableReason:String?
    
    /// 发票已选中的状态
    public var isselected = 0
    
    public var hasPdf:Bool?
    
    // MARK: SwiftyJSON Initializers
    /// Initiates the instance based on the object.
    ///
    /// - parameter object: The object of either Dictionary or Array kind that was passed.
    /// - returns: An initialized instance of the class.
    public init(object: Any) {
        self.init(json: JSON(object))
    }
    
    /// Initiates the instance based on the JSON that was passed.
    ///
    /// - parameter json: JSON object from SwiftyJSON.
    public init(json: JSON) {
        
        sCheckInfo = json[SerializationKeys.sCheckInfo].string
        fCheckFailMessage = json[SerializationKeys.fCheckFailMessage].string
        
        fBuyeraddress = json[SerializationKeys.fBuyeraddress].string
        fBuyeracount = json[SerializationKeys.fBuyeracount].string
        fUseState = json[SerializationKeys.fUseState].string
        fInvoicetypeNm = json[SerializationKeys.fInvoicetypeNm].string
        fSalername = json[SerializationKeys.fSalername].string
        fInvoicecode = json[SerializationKeys.fInvoicecode].string
        fInvoicedate = json[SerializationKeys.fInvoicedate].string ?? Date.today
        fOwnerId = json[SerializationKeys.fOwnerId].string
        fBuyertaxno = json[SerializationKeys.fBuyertaxno].string
        idKey = json[SerializationKeys.idKey].int.isSome ? "\(json[SerializationKeys.idKey].int!)" : json[SerializationKeys.idKey].string
        fTaxamount = json[SerializationKeys.fTaxamount].double.isSome ? "\(json[SerializationKeys.fTaxamount].double!)" : json[SerializationKeys.fTaxamount].string
        
        
        fSaleraddress = json[SerializationKeys.fSaleraddress].string
        fSaleracount = json[SerializationKeys.fSaleracount].string
        fInvoiceamount = json[SerializationKeys.fInvoiceamount].double.isSome ? "\(json[SerializationKeys.fInvoiceamount].double!)" : json[SerializationKeys.fInvoiceamount].string
        
        
        fOwnerNm = json[SerializationKeys.fOwnerNm].string
        fLegalizestate = json[SerializationKeys.fLegalizestate].string
        fPassenger = json[SerializationKeys.fPassenger].string
        fCreatetime = json[SerializationKeys.fCreatetime].string
        fSalertaxno = json[SerializationKeys.fSalertaxno].string
        fInvoiceno = json[SerializationKeys.fInvoiceno].string
        fTrainnumber = json[SerializationKeys.fTrainnumber].string
        fBuyername = json[SerializationKeys.fBuyername].string
        fArrivecity = json[SerializationKeys.fArrivecity].string
        fInvoicestatus = json[SerializationKeys.fInvoicestatus].string
        fDepartcity = json[SerializationKeys.fDepartcity].string
        fInvoicetypeId = json[SerializationKeys.fInvoicetypeId].string
        fLegalizedate = json[SerializationKeys.fLegalizedate].string
        fIseinvoice = json[SerializationKeys.fIseinvoice].string
        fTotalamount = json[SerializationKeys.fTotalamount].double.isSome ? "\(json[SerializationKeys.fTotalamount].double!)" : json[SerializationKeys.fTotalamount].string
        
        
        
        fCheckstate = json[SerializationKeys.fCheckstate].int.isSome ? "\(json[SerializationKeys.fCheckstate].int!)" : json[SerializationKeys.fCheckstate].string
        fVerifycode = json[SerializationKeys.fVerifycode].string
        resultTip = json[SerializationKeys.resultTip].string
        resultCode = json[SerializationKeys.resultCode].string
        fRecordnum = json[SerializationKeys.fRecordnum].string
        fInvoiceId = json[SerializationKeys.fInvoiceId].int.isSome ? "\(json[SerializationKeys.fInvoiceId].int!)" : json[SerializationKeys.fInvoiceId].string
        fRemark = json[SerializationKeys.fRemark].string
        fCompany = json[SerializationKeys.fCompany].int
        fBxtypeId = json[SerializationKeys.fBxtypeId].int ?? 6
        fBxtypeNm = json[SerializationKeys.fBxtypeNm].string ?? "其他"
        vouType = json[SerializationKeys.vouType].string
        inputNum = json[SerializationKeys.inputNum].string
        fOrgId = json[SerializationKeys.fOrgId].string
        fOrgNm = json[SerializationKeys.fOrgNm].string
        sNo = json[SerializationKeys.sNo].string
        fLabel = json[SerializationKeys.fLabel].string
        fpNumber = json[SerializationKeys.fpNumber].string
        hasPdf = json[SerializationKeys.hasPdf].bool
        fTaxRate = json[SerializationKeys.fTaxRate].string
        
        if let items = json[SerializationKeys.details].array { details = items.map { TicketDetailModel(json: $0) } }
        
    }
    
    /// Generates description of the object in the form of a NSDictionary.
    ///
    /// - returns: A Key value pair containing all valid values in the object.
    public func dictionaryRepresentation() -> [String: Any] {
        var dictionary: [String: Any] = [:]
        if let value = fBuyeraddress { dictionary[SerializationKeys.fBuyeraddress] = value }
        if let value = fBuyeracount { dictionary[SerializationKeys.fBuyeracount] = value }
        if let value = fUseState { dictionary[SerializationKeys.fUseState] = value }
        if let value = fInvoicetypeNm { dictionary[SerializationKeys.fInvoicetypeNm] = value }
        if let value = fSalername { dictionary[SerializationKeys.fSalername] = value } else {
            dictionary[SerializationKeys.fSalername] = ""
        }
        if let value = fInvoicecode { dictionary[SerializationKeys.fInvoicecode] = value } else {
            dictionary[SerializationKeys.fInvoicecode] = ""
        }
        if let value = fInvoicedate {  dictionary[SerializationKeys.fInvoicedate] = value }
        if let value = fOwnerId { dictionary[SerializationKeys.fOwnerId] = value }
        if let value = fBuyertaxno { dictionary[SerializationKeys.fBuyertaxno] = value } else {
            dictionary[SerializationKeys.fBuyertaxno] = ""
        }
        if let value = idKey { dictionary[SerializationKeys.idKey] = value } else {
            dictionary[SerializationKeys.idKey] = ""
        }
        if let value = fTaxamount { dictionary[SerializationKeys.fTaxamount] = value }
        if let value = fSaleraddress { dictionary[SerializationKeys.fSaleraddress] = value }
        if let value = fSaleracount { dictionary[SerializationKeys.fSaleracount] = value }
        if let value = fInvoiceamount { dictionary[SerializationKeys.fInvoiceamount] = value }
        if let value = fOwnerNm { dictionary[SerializationKeys.fOwnerNm] = value }
        if let value = fLegalizestate { dictionary[SerializationKeys.fLegalizestate] = value }
        if let value = fPassenger { dictionary[SerializationKeys.fPassenger] = value }
        if let value = fCreatetime { dictionary[SerializationKeys.fCreatetime] = value }
        if let value = fSalertaxno { dictionary[SerializationKeys.fSalertaxno] = value } else {
            dictionary[SerializationKeys.fSalertaxno] = ""
        }
        if let value = fInvoiceno { dictionary[SerializationKeys.fInvoiceno] = value } else {
            dictionary[SerializationKeys.fInvoiceno] = ""
        }
        if let value = fTrainnumber { dictionary[SerializationKeys.fTrainnumber] = value }
        if let value = fBuyername { dictionary[SerializationKeys.fBuyername] = value }
        if let value = fArrivecity { dictionary[SerializationKeys.fArrivecity] = value }
        if let value = fInvoicestatus { dictionary[SerializationKeys.fInvoicestatus] = value }
        if let value = fDepartcity { dictionary[SerializationKeys.fDepartcity] = value }
        if let value = fInvoicetypeId { dictionary[SerializationKeys.fInvoicetypeId] = value }
        if let value = fLegalizedate { dictionary[SerializationKeys.fLegalizedate] = value }
        if let value = fIseinvoice { dictionary[SerializationKeys.fIseinvoice] = value }
        if let value = fTotalamount { dictionary[SerializationKeys.fTotalamount] = value }
        if let value = fCheckstate { dictionary[SerializationKeys.fCheckstate] = value }
        if let value = fVerifycode { dictionary[SerializationKeys.fVerifycode] = value }
        if let value = resultTip { dictionary[SerializationKeys.resultTip] = value }
        if let value = resultCode { dictionary[SerializationKeys.resultCode] = value }
        if let value = fRecordnum { dictionary[SerializationKeys.fRecordnum] = value }
        if let value = fRemark { dictionary[SerializationKeys.fRemark] = value }
        if let value = fCompany { dictionary[SerializationKeys.fCompany] = value }
        dictionary[SerializationKeys.fBxtypeId] = fBxtypeId
        dictionary[SerializationKeys.fBxtypeNm] = fBxtypeNm
        if let value = vouType { dictionary[SerializationKeys.vouType] = value }
        if let value = inputNum { dictionary[SerializationKeys.inputNum] = value }
        if let value = fOrgId { dictionary[SerializationKeys.fOrgId] = value }
        if let value = sNo { dictionary[SerializationKeys.sNo] = value }
        if let value = fOrgNm { dictionary[SerializationKeys.fOrgNm] = value }
        if let value = fLabel { dictionary[SerializationKeys.fLabel] = value }
        if let value = fpNumber { dictionary[SerializationKeys.fpNumber] = value }
        if let value = fInvoiceId { dictionary[SerializationKeys.fInvoiceId] = value } else {
            dictionary[SerializationKeys.fInvoiceId] = ""
        }
        if let value = details { dictionary[SerializationKeys.details] = value.map { $0.dictionaryRepresentation() } }
        if let value = fTaxRate { dictionary[SerializationKeys.fTaxRate] = value }
        
        return dictionary
    }
    
}


 
